<template>
    <div>
        <el-form :inline="true" style="text-align:center;">
            <el-form-item>
              <el-input v-model="scientificName" placeholder="Please enter the subject name"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="userName" placeholder="Please enter the subject name"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="success" @click="querySalary">Search topics</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="tableData" border style="width: 100%">
            <el-table-column prop="realName" label="realName" align="center"></el-table-column>
            <el-table-column prop="scientificName" label="scientificName" align="center"></el-table-column>
            <el-table-column prop="scientificTitle" label="scientificTitle" align="center"></el-table-column>
            <el-table-column prop="scientificNumber" label="scientificNumber" align="center"></el-table-column>
            <el-table-column prop="publishTime" label="publishTime" align="center"></el-table-column>
            <el-table-column prop="awardLevel" label="awardLevel" align="center"></el-table-column>
            <el-table-column prop="comment" label="comment" align="center"></el-table-column>
            <el-table-column prop="paperAddress" label="paperAddress" align="center">
                <template slot-scope="scope">
                    <el-link v-if="scope.row.paperAddress != ''" :href="scope.row.paperAddress">
                        <el-button type="success">Download files</el-button>
                    </el-link>
                </template>
            </el-table-column>
            <el-table-column prop="paperAddress" label="operation" align="center">
                <template slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" circle @click="updateScience(scope.row.id)"></el-button>
                    <el-button type="danger" icon="el-icon-delete" circle @click="deleteScience(scope.row.id)"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
        <scienceDialog v-if="addScienceDialogVisible" ref="scienceDialog" @refreshDataList="querySalary"></scienceDialog>
    </div>
</template>

<script>
  import scienceDialog from './scienceDialog'
  export default {
    data () {
      return {
        tableData: [
          {
            realName: '',
            scientificName: '',
            scientificTitle: '',
            scientificNumber: '',
            publishTime: '',
            awardLevel: '',
            comment: ''
          }
        ],
        scientificName: '',
        userName: '',
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        addScienceDialogVisible: false
      }
    },
    components: {
      scienceDialog
    },
    activated () {
      this.querySalary()
    },
    methods: {
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.querySalary()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.querySalary()
      },
      // 获取科研管理表信息
      querySalary () {
        this.$http({
          url: this.$http.adornUrl('/edu/teduscientific/queryPageData/admin'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'scientificName': this.scientificName,
            'userName': this.userName
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.tableData = data.data.list
            this.totalPage = data.data.total
          } else {
            this.tableData = []
            this.totalPage = 0
          }
        })
      },
      // 发表课题
      addScienceDialog () {
        this.addScienceDialogVisible = true
        this.$nextTick(() => {
          this.$refs.scienceDialog.init()
        })
      },
      // 更新课题
      updateScience (id) {
        this.addScienceDialogVisible = true
        this.$nextTick(() => {
          this.$refs.scienceDialog.init(id)
        })
      },
      // 删除课题
      deleteScience (id) {
        this.$http({
          url: this.$http.adornUrl(`/edu/teduscientific/` + id),
          method: 'delete'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message.success('Deletion succeeded!')
            this.querySalary()
          } else {
            this.$message.error(data.msg)
          }
        })
      }
    }
  }
</script>
